package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    DishMapper dishMapper;

    // 添加购物车
    @Override
    public Result add(ShoppingCartDTO shoppingCartDTO) {

        if (shoppingCartDTO ==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        // 查询购物车表中，是否有该数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart = shoppingCart.toBuilder()
                .userId(BaseContext.getCurrnetId())
                .build();
        ShoppingCart isYou = shoppingCartMapper.getIsYou(shoppingCart);

        //没有新增
        if (isYou ==null){
            // 判断是否为菜品
            if (shoppingCartDTO.getSetmealId()==null){
                Dish dish = dishMapper.GetById(shoppingCartDTO.getDishId());
                shoppingCart= shoppingCart.toBuilder()
                        .name(dish.getName())
                        .image(dish.getImage())
                        .number(1)
                        .amount(dish.getPrice())
                        .createTime(LocalDateTime.now()).build();
                //添加数据库
                shoppingCartMapper.add(shoppingCart);
            }
            // 不是菜品，为套餐
            else {
                Setmeal  setmeal= setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart= shoppingCart.toBuilder()
                        .name(setmeal.getName())
                        .image(setmeal.getImage())
                        .number(1)
                        .amount(setmeal.getPrice())
                        .createTime(LocalDateTime.now()).build();
                //添加数据库
                shoppingCartMapper.add(shoppingCart);


            }
        // 有数据新增
        }else {
            isYou.setNumber(isYou.getNumber()+1);
            shoppingCartMapper.updateByNumber(isYou);
        }

        return Result.success();

    }

    @Override
    public Result getByUserId() {
        return Result.success(shoppingCartMapper.getByUserId(BaseContext.getCurrnetId()));
    }

    @Override
    public Result deleteByDishOrSetmealById(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.toBuilder()
                .userId(BaseContext.getCurrnetId());

        ShoppingCart isYou = shoppingCartMapper.getIsYou(shoppingCart);
        if (isYou.getNumber() ==1){
            shoppingCartMapper.deleteByDishOrSetmealById(isYou);
        }else {
            isYou.setNumber(isYou.getNumber()-1);
            shoppingCartMapper.updateByNumber(isYou);
        }
        return Result.success();
    }

    @Override
    public Result clean() {
        shoppingCartMapper.cleanByUserId(BaseContext.getCurrnetId());
        return Result.success();
    }




}
